# coding=utf-8 from __future__ import print_function, absolute_import, unicode_literals import decimal # import logging # from log import logger # import threading # import schedule # import time import datetime from gm.api import * # 将纯数字代码转化为=》掘金格式股票代码 def format_stock_symbol(code): if code.startswith('00'): return "SZSE." + code elif code.startswith('60'): return "SHSE." + code else: print(f"这个代码有异常,无法处理{code}") return code # def format_stock_symbol(code): # if code.startswith('00'): # return "'SZSE." + code + "'" # elif code.startswith('60'): # return "'SHSE." + code + "'" # else: # print(f"这个代码有异常,无法处理{code}") # return code # 将掘金格式股票代码转化=》纯数字代码 def format_stock_code(symbol): stock_code = symbol.split('.')[1] # 将symbol转为纯数字编号股票代码 return stock_code # 查询前N个交易日的具体交易日期函数:在调用时date_of_the_day应该传入当天日期,num是前跨天数。循环取值后 返回的date_of_the_day值就是目标具体的交易日期, def pre_num_trading_day(date_of_the_day, num): for i in range(num): Pre_date = get_previous_trading_date(exchange='SZSE', date=date_of_the_day) # 获取前一个交易日API date_of_the_day = Pre_date return date_of_the_day # 瞬时成交量计算公式 # 初始化历史价格 # last_volume = {} # def instantaneous_trading_volume(symbol,current_volume): # try: # if symbol not in current_volume: # return 0 # 不足两个历史价格,无法计算涨幅 # last_volume = current_volume[symbol] # instant_volume = current_volume - last_volume # 计算瞬时成交量 # return instant_volume # finally: # current_volume[symbol] = current_volume # 计算当日涨幅公式 def intraday_growth(price, pre_close): today_growth = (price - pre_close) / pre_close * 100 # 计算涨幅百分比 today_growth = round(today_growth,2) return today_growth # 计算实时涨幅公式 # 初始化历史价格 price_history = {} def calculate_growth(symbol, price): try: if symbol not in price_history: return 0 # 不足两个历史价格,无法计算涨幅 last_price = price_history[symbol] # print(f"price_history[symbol]~~~~~~~~~{price_history[symbol]}") growth = (price - last_price) / last_price * 100 # 计算涨幅百分比 return growth finally: price_history[symbol] = price # print(f"price======={price}") # print(f"price_history=={price_history}") # print(f"price_history[symbol]=={price_history[symbol]}") # 列表添加新元素的函数 def add_element(list, new_element): list.append(new_element) return list # 计算当日涨停价函数 形参pre_close 实参应传入 pre_close 【return的结果需要小数点后两位,在第三位进行四舍五入】 def limit_up_price(pre_close): limit_up_price = decimal.Decimal(str(pre_close)) * decimal.Decimal("1.1") limit_up_price = limit_up_price.quantize(decimal.Decimal("0.00"), decimal.ROUND_HALF_UP) return float(limit_up_price) # 计算当日跌停价函数 形参pre_close 实参应传入 pre_close 【return的结果需要小数点后两位round取整】 def limit_down_price(pre_close): limit_down_price = decimal.Decimal(str(pre_close)) * decimal.Decimal("0.9") limit_down_price = limit_down_price.quantize(decimal.Decimal("0.00"), decimal.ROUND_HALF_UP) return limit_down_price # print(limit_up_price(24.95)) # print(calculate_growth("000333",54.3)) # print(calculate_growth("000333",55.3)) # print(calculate_growth("000333",59.3)) # 安全交易量公式 用于计算不同时间段理论的安全交易量值 def secure_volume(now): # now = datetime.datetime.now() hour = now.hour minute = now.minute # 给今昨交易量比赋初值,然后每个时间段给具体的值 ratios = 2 if (hour >= 9 and minute >= 30) and (hour >= 9 and minute < 40) and hour < 10: ratios = 0.1 elif (hour >= 9 and minute >= 40) and hour < 10: ratios = 0.2 elif hour >= 10 and hour < 11: ratios = 0.4 elif hour >= 11 and hour < 14: ratios = 0.8 elif hour >= 14 and hour < 15: ratios = 1.2 return ratios